<?php

/**
 * Class de base qui gere les exceptions levees par le framework
 *
 * @category   McCube
 * @package    Root
 * @author     Leiha Sellier <leiha.sellier@gmail.com>
 * @license    http://creativecommons.org/licenses/by/3.0/  CC BY 3.0
 * @link       http://code.google.com/p/mechanics-cubes/
 */
class McCube_Exception
    extends    Exception
    implements McCube_Exception_Interface {

    protected $_title = 'Exception';
    protected $_richTitle = '[@error@Exception@]';
    protected $_displayBacktraces = true;

    /** */
    public static function handler($exception){
        if(!McCube_Notifier::isRenderOn())
            echo $exception->getTitle(false).' : '.$exception->getMessage();

        McCube_Notifier::render($exception->getTitle()
            , $exception->getMessage()
            , $exception->isMustDisplayBacktraces() ? $exception->getTrace() : false
        );
    }

    public function isMustDisplayBacktraces(){
        return $this->_displayBacktraces;
    }

    public function __construct($message, $errorCode, $extras=array(), $displayBacktraces=true){
        parent::__construct($message, $errorCode);
        $this->_displayBacktraces = $displayBacktraces;
    }

    public function getTitle($rich=true){
        return $rich ? $this->_richTitle : $this->_title;
    }

    public function getMcCode(){
        if(!isset($this->errorCode))
            return $this->getCode();

        return $this->errorCode;
    }

    public function getLitteralCode(){
        $txt = '';
        switch($this->getMcCode()){
            case -404 : $txt = 'Miss !';break;
            case 404  : $txt = 'Not Found !';break;
        }
        return $txt;
    }

}